package cola.rh.System.common.result;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Name: Result
 * @Author: Cola
 * @Time: 2023/3/6 11:25
 * @Description: Result
 */
@ApiModel("Result 结果集")
@Data
public class Result<T> {

    @ApiModelProperty("状态码")
    /*
    状态码
    */
    private Integer code;
    @ApiModelProperty("返回信息")
    /*
    返回信息
    */
    private String message;
    @ApiModelProperty("数据")
    /*
    数据
    */
    private T data;

    /*
    私有化
    */
    private Result() {
    }

    /**
     * 封装返回数据
     *
     * @param body           数据
     * @param resultCodeEnum resultCodeEnum
     * @param <T>            T
     * @return Result
     */
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();
        /*
        封装数据
        */
        if (body != null) {
            result.setData(body);
        }
        /*
        状态码
        */
        result.setCode(resultCodeEnum.getCode());
        /*
        返回信息
        */
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    /**
     * 成功(不带数据)
     *
     * @param <T> T
     * @return Result
     */
    public static <T> Result<T> ok() {
        return build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 成功(带数据)
     *
     * @param data 数据
     * @param <T>  T
     * @return Result
     */
    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    /**
     * 失败(不带数据)
     *
     * @param <T> T
     * @return Result
     */
    public static <T> Result<T> fail() {
        return build(null, ResultCodeEnum.FAIL);
    }

    /**
     * 失败(带数据)
     *
     * @param data 数据
     * @param <T>  T
     * @return Result
     */
    public static <T> Result<T> fail(T data) {
        return build(data, ResultCodeEnum.FAIL);
    }

    public Result<T> message(String msg) {
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }
}

